<template>
  <div class="dashboard-container" :style="backgroundDiv">
    <panel-group 
      :panel-data="panelData" 
      @handleSetLineChartData="handleSetLineChartData" 
    />

    <el-row class="line-chart-container">
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32" class="charts-row">
  <el-col :xs="24" :sm="24" :lg="8">
    <div class="chart-wrapper">
      <div class="chart-title">
        <i class="el-icon-data-analysis"></i>
        <span>奶茶产品分析</span>
      </div>
      <raddar-chart :radar-data="radarChartData" v-loading="loading" />
    </div>
  </el-col>
  <el-col :xs="24" :sm="24" :lg="8">
    <div class="chart-wrapper">
      <div class="chart-title">
        <i class="el-icon-pie-chart"></i>
        <span>奶茶销售占比</span>
      </div>
      <pie-chart :pie-data="pieChartData" v-loading="loading" />
    </div>
  </el-col>
  <el-col :xs="24" :sm="24" :lg="8">
    <div class="chart-wrapper">
      <div class="chart-title">
        <i class="el-icon-data-line"></i>
        <span>奶茶周销量统计</span>
      </div>
      <bar-chart :bar-data="barChartData" v-loading="loading" />
    </div>
  </el-col>
</el-row>
  </div>
</template>

<script>
import PanelGroup from "./components/PanelGroup";
import RaddarChart from "./components/RaddarChart";
import PieChart from "./components/PieChart";
import LineChart from "./components/LineChart";
import BarChart from "./components/BarChart";
import { getDashboardData } from "@/api/modules/app/dashboard";
// const lineChartData = {
//   newVisitis: {
//     expectedData: [100, 120, 161, 134, 105, 160, 165],
//     actualData: [120, 82, 91, 154, 162, 140, 145],
//   },
//   messages: {
//     expectedData: [200, 192, 120, 144, 160, 130, 140],
//     actualData: [180, 160, 151, 106, 145, 150, 130],
//   },
//   purchases: {
//     expectedData: [80, 100, 121, 104, 105, 90, 100],
//     actualData: [120, 90, 100, 138, 142, 130, 130],
//   },
//   shoppings: {
//     expectedData: [130, 140, 141, 142, 145, 150, 160],
//     actualData: [120, 82, 91, 154, 162, 140, 130],
//   },
// };

export default {
  name: "DashboardAdmin",
  components: {
    // GithubCorner,
    PanelGroup,
    RaddarChart,
    LineChart,
    PieChart,
    BarChart,
  },
  data() {
    return {
      loading: false,
      backgroundDiv: {
        backgroundRepeat: "no-repeat",
        backgroundSize: "cover",
        backgroundPosition: "center",
      },
      panelData: {},
      lineChartData: {},
      radarChartData: {},
      pieChartData: [],
      barChartData: {}
    };
  },
  created() {
    this.fetchDashboardData();
  },
  methods: {
    async fetchDashboardData() {
      this.loading = true;
      try {
        const { data } = await getDashboardData();
        if (data) {
          // 设置面板数据
          this.panelData = data.panelGroup || {};
          
          // 设置折线图初始数据
          if (data.lineChartData && data.lineChartData.newVisitis) {
            this.lineChartData = data.lineChartData.newVisitis;
          }
          
          // 保存完整的折线图数据，供点击面板时切换
          this.fullLineChartData = data.lineChartData || {};
          
          // 设置其他图表数据
          this.radarChartData = data.radarChartData || {};
          this.pieChartData = data.pieChartData || [];
          this.barChartData = data.barChartData || {};
        }
      } catch (error) {
        console.error('获取仪表盘数据失败:', error);
        this.$message.error('获取仪表盘数据失败');
      } finally {
        this.loading = false;
      }
    },
    handleSetLineChartData(type) {
      if (this.fullLineChartData && this.fullLineChartData[type]) {
        this.lineChartData = this.fullLineChartData[type];
      }
    },
    // 切换主题
    changeTheme(background) {
      window.localStorage.setItem("background", background);
      location.reload();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 24px;
  min-height: calc(100vh - 84px);
  position: relative;
  transition: all 0.3s;
}

.line-chart-container {
  background: #fff;
  padding: 24px;
  margin-bottom: 32px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
    transform: translateY(-5px);
  }
}

.charts-row {
  margin-bottom: 24px;
}

.chart-wrapper {
  background: #fff;
  padding: 24px;
  border-radius: 8px;
  position: relative;
  height: 100%;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
    transform: translateY(-5px);
  }
}

.chart-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  
  i {
    margin-right: 8px;
    color: #409EFF;
    font-size: 20px;
  }
}

@media (max-width: 1024px) {
  .dashboard-container {
    padding: 16px;
  }
  
  .line-chart-container, .chart-wrapper {
    padding: 16px;
  }
}
</style>
